﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using Jy123.Common;
using System.Collections.Generic;

namespace Jy123.Web.home
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Login : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strUserID = Cookie.getCookie("UserID");

            //string retVal = "";
            string email = context.Request["email"];
            string UserPass = context.Request["psw"];
            string strAutoLogin = context.Request["autologin"];

            if (!string.IsNullOrEmpty(email) & !string.IsNullOrEmpty(UserPass))
            {
                Jy123.BLL.User.UserSql bllUserSql = new Jy123.BLL.User.UserSql();
                //登陆状态
                int userID = 0;
                string userName = string.Empty;
                string userPortrait = string.Empty;
                int loginNum = 0;
                Jy123.BLL.User.EnumLoginState state = bllUserSql.Login(email, UserPass, out userID, out userName, out userPortrait, out loginNum);
                if (state == Jy123.BLL.User.EnumLoginState.Succeed)
                {
                    if (strAutoLogin == "on")
                    {
                        Cookie.SetCookie("UserID", userID.ToString(), 30);
                        Cookie.SetCookie("UserName", userName, 30);
                        Cookie.SetCookie("UserEmail", email, 30);
                        //return true;
                    }
                    else
                    {
                        Cookie.SetCookie("UserID", userID.ToString());
                        Cookie.SetCookie("UserName", userName);
                        Cookie.SetCookie("UserEmail", email);
                        //return true;
                    }
                }
                
                string reUrl = context.Request["url"].ToString();
                string strModule = string.Empty;
                if (reUrl.Contains("Module"))
                {
                    Jy123.BLL.User.Friend BLLUser = new Jy123.BLL.User.Friend();
                    int userId = BLLUser.SearchUserIDbyEmail(email);
                    Jy123.BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                    List<Jy123.Model.Module_Main> listModule = BLLModule.GetModelList("FUser = " + userId.ToString());
                    if (listModule.Count > 0)
                    {
                        strModule = "Select";
                    }
                    else
                    {
                        strModule = "Create";
                    }
                }
                context.Response.ContentType = "application/json";
                context.Response.Write(state.ToString()+strModule);
            }
            else
            {
                //写出主HTML页面
                string reUrl = "/home";
                try
                {
                    reUrl = context.Request.QueryString["url"].ToString();
                }
                catch
                { }
                string LoginHtml = context.Cache["user_login"].ToString();
                LoginHtml = LoginHtml.Replace("$url$", reUrl);

                //context.Response.ContentType = "text/plain";
                context.Response.ContentType = "text/html";
                context.Response.Write(LoginHtml);////////////////////
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
